package com.douyu.ocean.serversCheck;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;


public class CheckRedisQueue extends ServerCheck {
	private String host = "";
	private int port = 6379;
	private Map<String, Integer> queue = new HashMap<String, Integer>(); 
	private static Logger log = LoggerFactory.getLogger(CheckRedisQueue.class);
	private Jedis jedis;
	
	public CheckRedisQueue(String host, int port,Map<String, Integer> queue){
		this.host = host;
		this.port = port;
		this.queue = queue;
		this.jedis = new Jedis(this.host, this.port);
	}

	@Override
	public String checkHealth() {
			
		log.info("Checking redis server : "+this.host);
		String warning_message = "";
		for(Map.Entry<String, Integer> entry : queue.entrySet()){
			long len = jedis.llen(entry.getKey());
			log.info("redis queue : "+ entry.getKey() +", num :"+len);
			
			if(len >= entry.getValue()){
				String name = entry.getKey().substring(5);
				long over = len - entry.getValue();
				
				for(int i=10; i>0; i--){
					if(len >= entry.getValue() * i){
						warning_message = "Redis : "+this.host +",queue : "+name+",over : "+ over +";&"+i;
						break;
					}
				}
				
				log.warn(warning_message);
			}
		}
		return warning_message;
	}
}
